<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在字符串中查找字符串</title>
</head>

<body>
    <p id="p1">Click the button to locate where "locate" first occurs.</p>
    <p id="p2">0</p>
    <button onclick="myFunction()">点我</button>
    <script>
        function myFunction() {
            var str = document.getElementById("p1").innerHTML;
            var n = str.indexOf("locate");
            document.getElementById("p2").innerHTML = n + 1;
        }
    </script>
</body>

</html>